﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using DIWAGIS.Class;
using DIWAGIS.Modules;


namespace DIWAGIS.ApplicationLayer.QuanLyMangCapNuoc
{
    public partial class frmOngNhanh : Form
    {
        public frmOngNhanh()
        {
            InitializeComponent();
        }
        public IFeature pFeature;
        private ConfigInterface controller;
        Boolean isLoadComBox = false;
        string sTableName = generalVariable.database + "." + generalVariable.schema + "." + ClassVariables.wOngNhanh;
        private void frmOngNhanh_Load(object sender, EventArgs e)
        {
            controller = new ConfigInterface();
            DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
            cbx.loadDataDomainToCombobox(VatLieuOngNhanh, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "VL_ONHANH");
            cbx.LoadSubTypeToCombobox(CoOng, sTableName, "CoOng", DIWAGIS.Modules.generalVariable.ICurrentWorkspace);
            cbx.loadDataDomainToCombobox(TieuChuan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMTieuChuanOngNuoc");
            cbx.loadDataDomainToCombobox(Hieu, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMHieuOngNuoc");
            cbx.loadDataDomainToCombobox(TinhTrang, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMTinhTrangOngNuoc");
            cbx.loadDataDomainToCombobox(VatLieuVan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMVatLieuVan");

            cbx.loadDataDomainToCombobox(HieuVan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMHieuVan");
            cbx.loadDataDomainToCombobox(DuongKinhVan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMDuongKinhVan");
            cbx.loadDataDomainToCombobox(NuocSanXuat, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMNuocSanXuatOngNuoc");
            SqlCommand sqlcommand = new SqlCommand();


            sqlcommand.CommandText = "SELECT TENCONDUONG,TENCONDUONG FROM " + generalVariable.database + "." + generalVariable.schema + "." + " CONDUONG ";
            cbx.loadDataToCombobox(TenDuong, sqlcommand);

            sqlcommand.CommandText = "SELECT TENHUYEN,TENHUYEN FROM HUYEN ";
            cbx.loadDataToCombobox(Huyen, sqlcommand);

            isLoadComBox = true;
          
            if (pFeature.get_Value(pFeature.Fields.FindField("Phuong")) != null)
            {
                Phuong.SelectedValue = pFeature.get_Value(pFeature.Fields.FindField("Phuong"));
            }


            DIWAGIS.Class.general_Model p = new DIWAGIS.Class.general_Model();
            p.loadDataIntoForm(this, pFeature);
        }

        private void MaQuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoadComBox == true)
            {
                string strWhere = "";
                if (!string.IsNullOrEmpty(Huyen.Text))
                {
                    strWhere = Huyen.SelectedValue.ToString();
                    SqlCommand sqlcommand = new SqlCommand();
                    DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
                    //sqlcommand = new System.Data.SqlClient.SqlCommand();
                    sqlcommand.CommandText = "SELECT TenHanhChinh,TenHanhChinh FROM " + generalVariable.database + "." + generalVariable.schema + "." + "HANHCHINH" + " WHERE TenHuyen = @TenHuyen";
                    sqlcommand.Parameters.AddWithValue("@TenHuyen", strWhere);
                    cbx.loadDataToCombobox(Phuong, sqlcommand);
                    //loadDataToCombobox(cbxMaPhuong, sqlcommand);

                }
            }
        }

        private void buttonLuu_Click(object sender, EventArgs e)
        {
            IWorkspaceEdit pWorkSpaceEdit;
            pWorkSpaceEdit = generalVariable.ICurrentWorkspace as IWorkspaceEdit;
            pWorkSpaceEdit.StartEditing(true);
            pWorkSpaceEdit.StartEditOperation();

            DIWAGIS.Class.general_Model p = new DIWAGIS.Class.general_Model();
            p.setDataIntoDatatable(this, pFeature);

            pFeature.Store();
            pWorkSpaceEdit.StopEditOperation();
            pWorkSpaceEdit.StopEditing(true);
            pWorkSpaceEdit = null;
            MessageBox.Show("Cập nhật thành công", generalVariable.nameProgram, MessageBoxButtons.OK);
            this.Close();
        }

        private void buttonDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       

        private void ChieuDai_TextChanged(object sender, EventArgs e)
        {
            controller.validateDoubleField((TextBox)sender);
        }
    }
}
